﻿using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Threading;
using LY.Manage.WebApp.Models;
using System.IO;
using log4net;

namespace LY.Manage.WebApp
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication :  SpringMvcApplication //System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            /*
            //测试设置引用
            HttpConfiguration config = GlobalConfiguration.Configuration;
            config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            */

            //读取Log4net 配置信息
            log4net.Config.XmlConfigurator.Configure();



            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


            #region 开启写日志的线程,异常信息
            //开启写日志的线程
            string fileLogPath = Server.MapPath("/Log/");
            ThreadPool.QueueUserWorkItem((a) =>
            {
                while (true)
                {
                    if (MyExceptionAttribute.ExceptionQueue.Count > 0)
                    {
                        Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();
                        if (ex == null)
                        {
                            //队列中没有数据,休息3秒钟
                            Thread.Sleep(3000);
                        }
                        else
                        {
                            ////创建文件 自己创建的文件
                            //string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                            //File.AppendAllText(fileLogPath + fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ex.ToString(), System.Text.Encoding.UTF8);

                            ILog logger = LogManager.GetLogger("errorMsg");
                            logger.Error(ex.ToString());
                        }
                    }
                    else
                    {
                        //队列中没有数据,休息3秒钟
                        Thread.Sleep(3000);
                    }
                }
            }, fileLogPath); 
            #endregion





        }
    }
}